<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
	String path = request.getContextPath();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>配置角色资源</title>
<script src="<%=path %>/js/formValidator-4.1.1.js" type="text/javascript" charset="UTF-8"></script>
<script src="<%=path %>/js/formValidatorRegex.js" type="text/javascript" charset="UTF-8"></script>
<script src="<%=path %>/js/DateTimeMask.js" type="text/javascript" charset="UTF-8"></script>
<script type="text/javascript">
	$(function(){
		initRes();
		//给所有资源组复选框添加全选功能
		$('.rgGroup').each(function(){
			$(this).click(function(){
				var tem = $(this);
				$(this).parent().parent().contents().find(":checkbox[name='checkItems']").each(function(){
					if(tem.attr('checked') == 'checked'){
						$(this).attr('checked',true);
					}else{
						$(this).attr('checked',false);
					}
				});
			});
		});
		checkRelesRes();
		//配置资源
		$('#confRoleRes').click(
			function() {
				var para = '';
				$("input[name='checkItems'][type='checkbox']").each(function(i) {
					if ($(this).attr("checked") == 'checked') {
						para += $(this).attr('id') + ",";
						bool = true;
					}
				});
				if (confirm("确认要重新配置该角色相关资源?")) {
					window.location.href = "<%=path%>/role/configRoleRes.htm?checkItems=" + para.substring(0,para.length-1)
							+ "&role.RId=" + $(this).attr('val');
				}
			});
	});
	//把所有有资源组的资源归类到相应资源组下
	function initRes(){
		var allGroupedRes = eval('(' + "${allGroupedRes}" + ')');
		var resName = '';
		var rg_id = '';
		$.each(allGroupedRes,function(i,content){
			resName = content['RESOURCE_NAME'];
			rg_id = content['RG_ID'];
			$('#'+rg_id).append("<li><ul><li><input id='" + content['R_ID'] + "' type='checkbox' name='checkItems'></input>" + resName + "</li></ul></li>");
		});
	};
	//设置该角色已有资源为checked
	function checkRelesRes(){
		var allGroupedRes = eval('(' + "${allRolesRes}" + ')');
		var r_id = '';
		$.each(allGroupedRes,function(i,content){
			r_id = content['R_ID'];
			$('#'+r_id).attr('checked',true);
		});
	};
</script>
<style type="text/css">
ul{ 
	list-style:none; 
	margin-left:-20px;
} 
</style>
</head>
<body>
	<form id="rForm" action="<%=path%>/role/configRoleRes.htm" method="post">
		<table style="width: 100%; border-collapse: collapse; border-color: #D4D4D4;">
			<tr>
				<td width="20%"></td>
				<td>
					<div style="height: 300px;overflow:auto;overflow-x:hidden;">
						<table style="width: 100%;border: outset 1pt;">
						<tr style="background-color: #DEF2FF">
					      <td align="center" colspan="2">角色:&nbsp;${role.roleName }&nbsp;&nbsp;&nbsp;&nbsp;已有资源</td>
					    </tr>
				    	<c:choose>
							<c:when test="${allResGroup == null || fn:length(allResGroup) <= 0}">
								<tr class="tr_nomal"><td colspan="2" style="text-align: center;">无已分组资源</td></tr>
							</c:when>
							<c:otherwise>
								<c:forEach items="${allResGroup }" var="res" varStatus="status">
									<c:if test="${status.index%2 == 0}"><tr class="tr_nomal"></c:if>
										<td width="50%">
											<ul id="${res['RG_ID'] }">
												<li>
													<input class="rgGroup" id="${res['rg_id']}" type="checkbox"></input>
													${res['RESOURCE_GROUP_NAME'] }
												<li>
											</ul>
										</td>
									<c:if test="${status.index+1 == fn:length(allResGroup) && fn:length(allResGroup)%2 != 0 }">
										<td width="50%"></td>
									</c:if>
									<c:if test="${status.index%2 != 0}"></tr></c:if>
								</c:forEach>
							</c:otherwise>
						</c:choose>
					</table>
					</div>
				</td>
				<td width="20%"></td>
			</tr>
			<tr height="10px">
				<td colspan="3"></td>
			</tr>
			<tr>
				<td width="20%"></td>
				<td>
					<div style="height: 250px;">
						<table style="width: 100%;border: outset 1pt;horizontal-align: right;">
							<tr style="background-color: #DEF2FF">
						      <td align="center" colspan="2">未分组资源</td>
						    </tr>
						    	<c:choose>
								<c:when test="${noGroupedRes == null || fn:length(noGroupedRes) <= 0}">
									<tr class="tr_nomal"><td colspan="2" style="text-align: center;" >无未分组资源</td></tr>
								</c:when>
								<c:otherwise>
									<c:forEach items="${noGroupedRes }" var="res" varStatus="status">
										<c:if test="${status.index%2 == 0}"><tr class="tr_nomal"></c:if>
											<td style="vertical-align:top;width: 50%"><input name='checkItems' type="checkbox" id="${res['R_ID'] }"></input>&nbsp;&nbsp;${res['RESOURCE_NAME'] }</td>
										<c:if test="${status.index+1 == fn:length(noGroupedRes) && fn:length(noGroupedRes)%2 != 0 }">
											<td width="50%"></td>
										</c:if>
										<c:if test="${status.index%2 != 0}"></tr></c:if>
									</c:forEach>
								</c:otherwise>
							</c:choose>
							<tr>
								<td></td>
								<td style="padding-left: 100px;"><input val="${role.RId }" id="confRoleRes" type="button" value="提交"></input></td>
							</tr>
						</table>
					</div>
				</td>
				<td width="20%"></td>
			</tr>
		</table>
	</form>
</body>
</html>